package argo.hot100.search2mid;

public class SearchRange {

    public int[] searchRange(int[] nums, int target) {

        int[] res = new int[]{-1, -1};

        if (nums == null || nums.length == 0) {
            return res;
        }

        int left = 0, right = nums.length - 1, mid;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                int i = mid, j = mid;
                while (i - 1 >= 0 && nums[i - 1] == target) {
                    i--;
                }
                while (j + 1 < nums.length && nums[j + 1] == target) {
                    j++;
                }
                res[0] = i;
                res[1] = j;
                break;
            }
        }
        return res;


    }
}
